home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / progwin.zip / WHELLO.ZIP / WHELLO.CPP next >
Text File  |  1991-02-13  |  9KB  |  275 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. //  Windows HELLO WORLD - C++ Version 
  4.  
  5. #include <windows.h>
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. long FAR PASCAL _export WndProc( HWND hWnd, WORD iMessage,
  11.                                  WORD wParam, LONG lParam );
  12.  
  13. class Main
  14. {
  15.     public:
  16.     static HANDLE hInstance;
  17.     static HANDLE hPrevInstance;
  18.     static int nCmdShow;
  19.     static int MessageLoop( void );
  20. };
  21. HANDLE Main::hInstance = 0;
  22. HANDLE Main::hPrevInstance = 0;
  23. int Main::nCmdShow = 0;
  24.  
  25. int Main::MessageLoop( void )
  26. {
  27.     MSG msg;
  28.  
  29.     while( GetMessage( &msg, NULL, 0, 0 ) )
  30.     {
  31.         TranslateMessage( &msg );
  32.         DispatchMessage( &msg );
  33.     }
  34.     return msg.wParam;
  35. }
  36.  
  37.  
  38. // Base class
  39. class Window
  40. {
  41.     protected:
  42.         HWND hWnd;
  43.     public:
  44.         // Provide (read) access to the window's handle in case it is needed
  45.         // elsewhere.
  46.         HWND GetHandle( void ) { return hWnd; }
  47.  
  48.         BOOL Show( int nCmdShow ) { return ShowWindow( hWnd, nCmdShow ); }
  49.         void Update( void ) { UpdateWindow( hWnd ); }
  50.         // Pure virtual function makes Window an abstract class.
  51.         virtual long WndProc( WORD iMessage, WORD wParam, LONG lParam ) = 0;
  52. };
  53.  
  54. class MainWindow : public Window
  55. {
  56.     private:
  57.         static char szClassName[14];
  58.         // Helper function used by Paint function; it is used as a
  59.         // callback function by LineDDA.
  60.         static void FAR PASCAL LineFunc( int X, int Y, LPSTR lpData );
  61.     public:
  62.         // Register the class only AFTER WinMain assigns appropriate
  63.         // values to static members of Main and only if no previous
  64.         // instances of the program exist (a previous instance would
  65.         // have already performed the registration).
  66.         static void Register( void )
  67.         {
  68.             WNDCLASS wndclass;   // Structure used to register Windows class.
  69.  
  70.             wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  71.             wndclass.lpfnWndProc   = ::WndProc;
  72.             wndclass.cbClsExtra    = 0;
  73.             // Reserve extra bytes for each instance of the window;
  74.             // we will use these bytes to store a pointer to the C++
  75.             // (MainWindow) object corresponding to the window.
  76.             // the size of a 'this' pointer depends on the memory model.
  77.             wndclass.cbWndExtra    = sizeof( MainWindow * );
  78.             wndclass.hInstance     = Main::hInstance;
  79.             wndclass.hIcon         = LoadIcon( Main::hInstance, "whello" );
  80.             wndclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
  81.             wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
  82.             wndclass.lpszMenuName  = NULL;
  83.             wndclass.lpszClassName = szClassName;
  84.  
  85.             if ( ! RegisterClass( &wndclass ) )
  86.                 exit( FALSE );
  87.         }
  88.  
  89.         // Do not create unless previously registered.
  90.         MainWindow( void )
  91.         {
  92.             // Pass 'this' pointer in lpParam of CreateWindow().
  93.             hWnd = CreateWindow( szClassName,
  94.                 szClassName,
  95.                 WS_OVERLAPPEDWINDOW,
  96.                 CW_USEDEFAULT,
  97.                 0,
  98.                 CW_USEDEFAULT,
  99.                 0,
  100.                 NULL,
  101.                 NULL,
  102.                 Main::hInstance,
  103.                 (LPSTR) this );
  104.             if ( ! hWnd )
  105.                 exit( FALSE );
  106.  
  107.             Show( Main::nCmdShow );
  108.             Update();
  109.         }
  110.         long WndProc( WORD iMessage, WORD wParam, LONG lParam );
  111.  
  112.         // Print a message in the client rectangle.
  113.         void Paint( void );
  114.         // Struct used by Paint to pass information to callback function
  115.         // used by LineDDA
  116.         struct LINEFUNCDATA
  117.         {
  118.             HDC hDC;
  119.             char FAR *LineMessage;
  120.             int MessageLength;
  121.             LINEFUNCDATA( char *Message )
  122.             {
  123.                 hDC = 0;
  124.                 MessageLength = strlen( Message );
  125.                 LineMessage = new char far [MessageLength+1];
  126.                 lstrcpy( LineMessage, Message );
  127.             };
  128.             ~LINEFUNCDATA( void ) { delete LineMessage; }
  129.         };
  130. };
  131. char MainWindow::szClassName[] = "Hello, World!";
  132.  
  133. void MainWindow::Paint( void )
  134. {
  135.     PAINTSTRUCT ps;
  136.     RECT rect;
  137.     FARPROC lpLineFunc;
  138.     LINEFUNCDATA LineFuncData( "Borland C++ welcomes you to the Wonderful World of Windows Programming!" );
  139.  
  140.     lpLineFunc = MakeProcInstance( (FARPROC) MainWindow::LineFunc, Main::hInstance );
  141.     BeginPaint( hWnd, &ps );
  142.     GetClientRect( hWnd, (LPRECT) &rect );
  143.     LineFuncData.hDC = ps.hdc;
  144.     SetTextAlign( ps.hdc, TA_BOTTOM );
  145.     LineDDA( 0, 0, 0,
  146.              rect.bottom / 2, lpLineFunc, (LPSTR) &LineFuncData );
  147.     EndPaint( hWnd, &ps );
  148.     FreeProcInstance( lpLineFunc );
  149. }
  150.  
  151. void FAR PASCAL MainWindow::LineFunc( int X, int Y, LPSTR lpData )
  152. {
  153.     LINEFUNCDATA FAR * lpLineFuncData = (LINEFUNCDATA FAR *) lpData;
  154.     TextOut( lpLineFuncData->hDC, X, Y,
  155.              lpLineFuncData->LineMessage, lpLineFuncData->MessageLength );
  156. }
  157.  
  158. long MainWindow::WndProc( WORD iMessage, WORD wParam, LONG lParam )
  159. {
  160.     switch (iMessage)
  161.     {
  162.         case WM_CREATE:
  163.         break;
  164.         case WM_PAINT:
  165.             Paint();
  166.             break;
  167.         case WM_DESTROY:
  168.             PostQuitMessage( 0 );
  169.             break;
  170.         default:
  171.             return DefWindowProc( hWnd, iMessage, wParam, lParam );
  172.     }
  173. }
  174.  
  175. // If data pointers are near pointers
  176. #if defined(__SMALL__) || defined(__MEDIUM__)
  177. inline Window *GetPointer( HWND hWnd )
  178. {
  179.     return (Window *) GetWindowWord( hWnd, 0 );
  180. }
  181. inline void SetPointer( HWND hWnd, Window *pWindow )
  182. {
  183.     SetWindowWord( hWnd, 0, (WORD) pWindow );
  184. }
  185.  
  186. // else pointers are far
  187. #elif defined(__LARGE__) || defined(__COMPACT__)
  188. inline Window *GetPointer( HWND hWnd )
  189. {
  190.     return (Window *) GetWindowLong( hWnd, 0 );
  191. }
  192. inline void SetPointer( HWND hWnd, Window *pWindow )
  193. {
  194.     SetWindowLong( hWnd, 0, (LONG) pWindow );
  195. }
  196.  
  197. #else
  198.     #error Choose another memory model!
  199. #endif
  200.  
  201.  
  202. long FAR PASCAL _export WndProc( HWND hWnd, WORD iMessage, WORD wParam,
  203.                                  LONG lParam )
  204. {
  205.     // Pointer to the (C++ object that is the) window.
  206.     Window *pWindow = GetPointer( hWnd );
  207.  
  208.     // The pointer pWindow will have an invalid value if the WM_CREATE
  209.     // message has not yet been processed (we respond to the WM_CREATE
  210.     // message by setting the extra bytes to be a pointer to the
  211.     // (C++) object corresponding to the Window identified
  212.     // by hWnd).  The messages that
  213.     // precede WM_CREATE must be processed without using pWindow so we
  214.     // pass them to DefWindowProc.
  215.     // How do we know in general if the pointer pWindow is invalid?
  216.     // Simple: Windows allocates the window extra bytes using LocalAlloc
  217.     // which zero initializes memory; thus, pWindow will have a value of
  218.     // zero before we set the window extra bytes to the 'this' pointer.
  219.     // Caveat emptor: the fact that LocalAlloc will zero initialize the
  220.     // window extra bytes is not documented; therefore, it could change
  221.     // in the future.
  222.  
  223.     if ( pWindow == 0 )
  224.     {
  225.         if ( iMessage == WM_CREATE )
  226.         {
  227.             LPCREATESTRUCT lpcs;
  228.  
  229.             lpcs = (LPCREATESTRUCT) lParam;
  230.             pWindow = (Window *) lpcs->lpCreateParams;
  231.  
  232.             // Store a pointer to this object in the window's extra bytes;
  233.             // this will enable to access this object (and its member
  234.             // functions) in WndProc where we are
  235.             // given only a handle to identify the window.
  236.             SetPointer( hWnd, pWindow );
  237.             // Now let the object perform whatever
  238.             // initialization it needs for WM_CREATE in its own
  239.             // WndProc.
  240.             return pWindow->WndProc( iMessage, wParam, lParam );
  241.         }
  242.         else
  243.             return DefWindowProc( hWnd, iMessage, wParam, lParam );
  244.     }
  245.     else
  246.         return pWindow->WndProc( iMessage, wParam, lParam );
  247. }
  248.  
  249. // Turn off warning: Parameter 'lpszCmdLine' is never used in function WinMain(unsigned int,unsigned int,char far*,int)
  250. #pragma argsused
  251.  
  252. // Turn off warning: 'MainWnd' is assigned a value that is never used in function WinMain(unsigned int,unsigned int,char far*,int)
  253. #pragma option -w-aus
  254.  
  255. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
  256.                     int nCmdShow )
  257. {
  258.     Main::hInstance = hInstance;
  259.     Main::hPrevInstance = hPrevInstance;
  260.     Main::nCmdShow = nCmdShow;
  261.  
  262.     // A Windows class should be registered with Windows before any windows
  263.     // of that type are created.
  264.     // Register here all Windows classes that will be used in the program.
  265.     // Windows classes should not be registered if an instance of
  266.     // the program is already running.
  267.     if ( ! Main::hPrevInstance ) {
  268.         MainWindow::Register();
  269.     }
  270.  
  271.     MainWindow MainWnd;
  272.  
  273.     return Main::MessageLoop();
  274. }
  275.